package com.serkol.ams.controller.api;


import com.serkol.ams.bean.Result;
import com.serkol.ams.entity.Settings;
import com.serkol.ams.entity.SysConfig;
import com.serkol.ams.service.SettingsService;
import com.serkol.ams.service.SysConfigService;
import com.serkol.annotation.IsLogin;
import com.serkol.constant.AuthConstant;
import com.serkol.constant.ResultConstant;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Hesenjan(Serkol)
 * @since 2021-11-17
 */

@RestController
@RequestMapping("api/cdn/sys/settings")
public class SysSettingsApiController {
    @Autowired
    private SettingsService service;


    @Value("${upload.path}")
    private String uploadpath;
    @Value("${upload.host}")
    private String uploadhost;
    @ApiOperation(value = "获取参数值")
    @GetMapping("info")
    public Result info() {
        AuthConstant.setPathUrl(uploadhost);
        Map<String, Object> list = service.getInfo();

        return Result.ok(list);
    }


    @IsLogin
    @PostMapping("edit")
    public Result edit(@RequestBody Settings data) {

data.setId(1L);
        boolean flag = false;
        if (data.getId() != null && data.getId() > 0) {

            flag = service.updateById(data);
        } else {
            flag = service.save(data);
        }
        if(flag){
            service.getInfo();
        }

        return flag ? Result.ok() : Result.failure(ResultConstant.FAIL);


    }


//myInfo

    @ApiOperation(value = "获取参数值")
    @GetMapping("myInfo")
    public Result myInfo() {
        if (AuthConstant.getSettingsInfo() == null) {
            AuthConstant.setPathUrl(uploadhost);
            Map<String, Object> list = service.getInfo();
            return Result.ok(list);
        } else {
            return Result.ok(AuthConstant.getSettingsInfo());
        }

    }


}

